package cn.xutongxue.gtms.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.xutongxue.gtms.base.DeleteRequest;
import cn.xutongxue.gtms.base.Errors;
import cn.xutongxue.gtms.base.Result;
import cn.xutongxue.gtms.constant.UserConstant;
import cn.xutongxue.gtms.entity.Thesis;
import cn.xutongxue.gtms.entity.dto.thesis.PostThesisRequest;
import cn.xutongxue.gtms.entity.dto.thesis.ThesisQueryRequest;
import cn.xutongxue.gtms.entity.dto.thesis.UpdateThesisRequest;
import cn.xutongxue.gtms.entity.vo.ApplyVo;
import cn.xutongxue.gtms.entity.vo.ThesisTeacherVo;
import cn.xutongxue.gtms.entity.vo.ThesisVo;
import cn.xutongxue.gtms.exception.BusinessException;
import cn.xutongxue.gtms.exception.ThrowUtils;
import cn.xutongxue.gtms.service.impl.ThesisServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/thesis")
@Api(tags = "论文管理")
@CrossOrigin
public class ThesisController {

    @Resource
    private ThesisServiceImpl thesisService;

    // teacher thesis crud region
    @PostMapping("/post")
    @ApiOperation("发布论文")
    @SaCheckRole(UserConstant.TEACHER_ROLE)
    public Result postThesis(@ApiParam(name = "论文信息") @RequestBody PostThesisRequest postThesisRequest, HttpServletRequest request) {
        if (postThesisRequest == null) {
            throw new BusinessException(Errors.PARAMS_ERROR);
        }
        return Result.success(thesisService.postThesis(postThesisRequest, request));
    }

    @PostMapping("/delete")
    @ApiOperation("删除论文")
    @SaCheckRole(UserConstant.TEACHER_ROLE)
    public Result deleteThesis(@ApiParam(name = "删除论文ID") @RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null) {
            throw new BusinessException(Errors.PARAMS_ERROR);
        }
        return Result.success(thesisService.deleteThesis(deleteRequest));
    }

    @PostMapping("/update")
    @ApiOperation("修改论文信息")
    @SaCheckRole(UserConstant.TEACHER_ROLE)
    public Result updateThesis(@ApiParam(name = "论文信息表单") @RequestBody UpdateThesisRequest updateThesisRequest) {
        if (updateThesisRequest == null || updateThesisRequest.getId() == null) {
            throw new BusinessException(Errors.PARAMS_ERROR);
        }
        String title = updateThesisRequest.getTitle();
        if (StringUtils.isAnyBlank(title)) {
            throw new BusinessException(Errors.PARAMS_ERROR);
        }

        Thesis thesis = new Thesis();
        BeanUtil.copyProperties(updateThesisRequest, thesis);
        ThrowUtils.throwIf(!thesisService.updateById(thesis), Errors.OPERATION_ERROR);
        return Result.success(true);
    }

    // teacher thesis crud end region

    @PostMapping("/list/page")
    @ApiOperation("分页查询论文")
    @SaCheckLogin
    public Result listThesisByPage(@ApiParam(name = "查询条件") @RequestBody ThesisQueryRequest thesisQueryRequest) {
        long size = thesisQueryRequest.getSize();
        ThrowUtils.throwIf(size > 100, Errors.PARAMS_ERROR);
        Page<ThesisVo> thesisVoPage = thesisService.listThesisByPage(thesisQueryRequest);
        return Result.success(thesisVoPage);
    }


    @GetMapping("/list")
    @ApiOperation("教师查询自己发布的论文")
    @SaCheckRole(UserConstant.TEACHER_ROLE)
    public Result listThesisByTeacherId(@RequestParam("id") Long id,
                                        @RequestParam("current") Integer current,
                                        @RequestParam("size") Integer size) {
        ThrowUtils.throwIf(size > 100, Errors.PARAMS_ERROR);
        Page<ThesisTeacherVo> thesisVoPage = thesisService.listThesisByTeacherId(id, current, size);
        return Result.success(thesisVoPage);
    }

    @PostMapping("/selectThesis")
    @ApiOperation("学生选择论文")
    @SaCheckRole(UserConstant.DEFAULT_ROLE)
    public Result selectThesis(@RequestParam("thesisId") Long thesisId) {
        if (thesisId == null) {
            throw new BusinessException(Errors.PARAMS_ERROR);
        }
        Boolean res = thesisService.selectThesis(thesisId);
        return Result.success(res);
    }


    @GetMapping("/applyList")
    @ApiOperation("教师审批列表")
    @SaCheckRole(UserConstant.TEACHER_ROLE)
    public Result applyList(@RequestParam("current") Integer current,
                            @RequestParam("size") Integer size) {
        ThrowUtils.throwIf(size > 100, Errors.PARAMS_ERROR);
        Long teacherId = StpUtil.getLoginIdAsLong();
        Page<ApplyVo> applyVoPage = thesisService.applyList(teacherId, current, size);
        return Result.success(applyVoPage);
    }

    @PostMapping("/audit")
    @ApiOperation("教师审批")
    @SaCheckRole(UserConstant.TEACHER_ROLE)
    public Result audit(@RequestParam("thesisId") Long thesisId,
                        @RequestParam("state") Integer state) {
        if (thesisId == null || state == null) {
            throw new BusinessException(Errors.PARAMS_ERROR);
        }
        Boolean res = thesisService.audit(thesisId, state);
        return Result.success(res);
    }

    @GetMapping("/getThesisInfo")
    @ApiOperation("获取论文信息")
    @SaCheckRole(UserConstant.DEFAULT_ROLE)
    public Result getThesisInfo() {
        return Result.success(thesisService.getThesisInfo());
    }

    @GetMapping("/getGuideList")
    @ApiOperation("获取已选课题学生列表")
    @SaCheckRole(UserConstant.TEACHER_ROLE)
    public Result getGuideList() {
        return Result.success(true);
    }
}
